package com.goodmean.student;

import com.goodmean.common.VO.studentGradeModel;
import com.goodmean.common.model.*;
import com.goodmean.teacher.admin.ClazzCourseService;
import com.goodmean.teacher.admin.CourseService;
import com.goodmean.teacher.admin.CourseStudentGradeService;
import com.goodmean.teacher.teacher.TeacherService;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentService {

    public static final StudentService me = new StudentService();
    private static final Student dao = new Student().dao();
    private int flag = 0;

    /**
     * 查询学生个人信息
     **/
    public Student findByid(String student_id) {
        return dao.findFirst("SELECT * FROM student WHERE student_id = ?", student_id);
    }
    /**
     * 查询学生个人成绩
     **/
    public List <studentGradeModel> findGrade(String student_id) {
        List <studentGradeModel> student = new ArrayList <>();
        List <CourseStudentGrade> list = CourseStudentGradeService.me.findBySid(student_id);
        for (CourseStudentGrade li : list) {
            studentGradeModel model = new studentGradeModel();
            Course course = CourseService.me.findById(li.getInt("course_id"));
            model.setCourse_name(course.getStr("course_name"));
            model.setCourse_id(li.getInt("course_id"));
            model.setCourse_term(course.getInt("course_term"));
            model.setCourse_grade(li.getInt("course_grade"));
            student.add(model);
        }
        return student;
    }

    /**
     *
     * 保存信息
     */
    public boolean saveM(Student student) {
        Student student1 = new Student();
        switch (student.getStudentSex()) {
            case "0":
                student1.setStudentSex("女");
                break;
            case "1":
                student1.setStudentSex("男");
        }
        student1.setStudentId(student.getStudentId());
        student1.setClazzId(student.getClazzId());
        student1.setStudentPhone(student.getStudentPhone());
        student1.setStudentAddress(student.getStudentAddress());
        return student1.update();
    }

    /**
     * 搜索课程表信息
     */
    public Map <Integer, String> selectCourse(String student_id) {
        Student student = findByid(student_id);
        Map <Integer, String> coursemap = setNull();
        List <ClazzCourse> course = ClazzCourseService.me.findByClazzId(student.getClazzId());
        for (ClazzCourse clc : course) {
            ClazzCourse clazzcourse = ClazzCourseService.me.findByCidBid(student.getClazzId(), clc.getCourseId());
            Teacher teacher = TeacherService.me.findById(clazzcourse.getTeacherId());
            Course cours = CourseService.me.findById(clc.getCourseId());
            String courseMe = cours.getCourseName() + "(" + teacher.getTeacherName() + ")" + "教室：" + clazzcourse.getCoursePlace();
            String[] coursetime = clazzcourse.getCourseTime().split(",");
            for (String coursetime1 : coursetime) {
                String[] coursetime2 = coursetime1.split("-");
                Integer coursetime3 = Integer.valueOf(coursetime2[0] + coursetime2[1]);
                coursemap.put(coursetime3, courseMe);
            }
        }
        return coursemap;
    }

    public Map <Integer, String> setNull() {
        Map <Integer, String> coursemap = new HashMap <>();
        for (int i = 1; i < 5; i++) {
            for (int j = 1; j < 8; j++) {
                int n = i * 10 + j;
                coursemap.put(n, "");
            }
        }
        return coursemap;
    }
    /**
     * 以班级为单位查询学生信息
     **/
    public Page <Student> findByClazzId(Integer clazz_id , String student_name , Integer pageNo) {
        Page <Student> paginate;
        String sql = "FROM student_info.student WHERE 1=1 AND clazz_id = ? AND student_name like '%" + student_name + "%'";
        paginate = dao.paginate(pageNo, 10, "SELECT *", sql, clazz_id);
        return paginate;
    }
    /**
     * 添加学生信息
     *
     * @param student 学生对象
     * @return boolean 是否添加成功
     */
    public boolean addStudent(Student student) {
        boolean canAdd = student.save();
        return canAdd;
    }


    /**
     * 更新学生信息
     *
     * @param student 学生对象
     * @return boolean 是否更新成功
     */
    public boolean updateStudent(Student student) {
        boolean canUpdate = student.update();
        return canUpdate;
    }

    /**
     * 根据教师编号删除教师信息
     *
     * @param studentId 学号
     * @return boolean 是否删除成功
     */
    public boolean deleteStudent(String studentId) {
        boolean canDelete = dao.deleteById(studentId);
        return canDelete;
    }
    /**
     * 通过clazz_id 查询这个班级里的人
     */
    public List<Student> findByClazzId(Integer clazzId){
        return dao.find("SELECT * FROM student WHERE clazz_id = ?", clazzId );
    }


    /**
     * 根据学生编号，学生名字查询学生信息
     *
     * @param studentId   学生信息
     * @param studentName 学生名字
     * @return List<Student> 学生信息
     */
    public Page <Student> findStudent(String studentId, String studentName, int page) {
        Page <Student> paginate;
        String sqlExceptSelect = "FROM student_info.student WHERE 1=1" + addWhere(studentId, studentName);
        switch (flag) {
            case (0):
                paginate = dao.paginate(page, 10, "SELECT *", sqlExceptSelect);
                break;
            case (1):
                paginate = dao.paginate(page, 10, "SELECT *", sqlExceptSelect, studentId);
                break;
            case (2):
                paginate = dao.paginate(page, 10, "SELECT *", sqlExceptSelect, studentName);
                break;
            default:
                paginate = dao.paginate(page, 10, "SELECT *", sqlExceptSelect, studentId, studentName);
                break;
        }
        flag = 0;
        return paginate;
    }

    public String addWhere(String studentId, String studentName) {
        StringBuffer where = new StringBuffer(" ");
        if (StrKit.notBlank(studentId)) {
            flag += 1;
            where.append("AND student_id = ? ");
        }
        if (StrKit.notBlank(studentName)) {
            flag += 2;
            where.append("AND student_name = ? ");
        }
        return where.toString();
    }

    /** 以学生班级和姓名查找 **/
    public List<Student> findByNameClass(String student_name , Integer class_id){
        String sql = "SELECT * FROM student WHERE  student_name like '%" + student_name + "%'";
        return dao.find(sql);
    }

}
